package com.example.netty.initializer;

import org.springframework.stereotype.Component;

import com.example.netty.handler.SocketHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import lombok.extern.slf4j.Slf4j;

/**
 * @Title: SocketInitializer.java
 * @Package com.example.netty.initializer
 * @Description Socket 初始化器，每一个Channel进来都会调用这里的 InitChannel 方法
 * @author feina
 * @date 2023/4/23 13:16
 * @Copyright
 */
@Slf4j
@Component
public class SocketInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
             // 添加对byte数组的编解码，netty提供了很多编解码器，你们可以根据需要选择
             pipeline.addLast(new ByteArrayDecoder());
             pipeline.addLast(new ByteArrayEncoder());
             // 添加上自己的处理器
             pipeline.addLast(new SocketHandler());
    }
}
